using System;
using System.Collections.Generic;

namespace Charisma.Tests.Models.Persons
{
  public class PersonContext : DbContext
  {
    public Repository<Person> Persons { get; set; }

    public void Recreate()
    {
      // TODO : bullshit!!!! I would like to have Database property within the DbContext
        this.Database.Drop();

      // Fill the database
      Persons.Add(new Person("Bob", "McBob"){
        MidName = "Bart", 
        Age = 42,
        EmployerIds = new[] { 1, 2 },
        PrimaryAddress = new Address { City = "London", IsInternational = true, AddressType = AddressType.Company },
        Addresses = new List<Address> {
          new Address { City = "London", IsInternational = true, AddressType = AddressType.Company },
          new Address { City = "Tokyo", IsInternational = true, AddressType = AddressType.Private }, 
          new Address { City = "Seattle", IsInternational = false, AddressType = AddressType.Private } 
      }});

      Persons.Add(new Person("Jane", "McJane") {
        Age = 35,
        EmployerIds = new[] { 1 },
        PrimaryAddress = new Address { City = "Paris", IsInternational = true, AddressType = AddressType.Private },
        Addresses = new List<Address> {
          new Address { City = "Paris", AddressType = AddressType.Private }
      }});

      Persons.Add(new Person("Joe", "McJoe") {
        Age = 21,
        EmployerIds = new[] { 3 },
        PrimaryAddress = new Address { City = "Chicago", IsInternational = false, AddressType = AddressType.Private },
        Addresses = new List<Address> {
          new Address { City = "Chicago", AddressType = AddressType.Private },
          new Address { City = "London", AddressType = AddressType.Company }
      }});
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
//        modelBuilder.Repository(context => context.Persons)
//            .HasName("persons");
//        .HasKeyGenerator(() => new StringKeyGenerator());
      modelBuilder.Entity<Person>()
        .Map(p => p.FirstName).HasName("fn")
        .Map(p => p.LastName).HasName("ln")
        .Map(p => p.Age).HasName("age")
        .Map(p => p.PrimaryAddress).HasName("add")
        .Map(p => p.Addresses).HasName("otherAdds")
        .Map(p => p.EmployerIds).HasName("emps");

      modelBuilder.Entity<Address>()
        .Map(a => a.City).HasName("city")
        .Map(a => a.IsInternational).HasName("international")
        .Map(a => a.AddressType).HasName("type");
    }
  }
}